package cn.fl.capital.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.multi.redis.service.IMultiRedisService;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.cache.framework.redis.RedisHelper;
import cn.fl.capital.condition.CapFundQueryCondition;
import cn.fl.capital.service.ICapFundDivService;
import cn.fl.capital.service.ICapFundService;
import cn.fl.capital.vo.CapFundVO;
import cn.fl.custpay.dto.DataResultDTO;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysUsrVO;
import com.alibaba.fastjson.JSON;
import com.lionbridge.activiti.util.redis.RedisLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 资金拆分
 * 作者:hongyanbo
 * 时间:2018/10/19
 */
@Controller
@RequestMapping("/capital/capFund/div")
public class CapFundDivController {
    public static final Logger LOGGER = LoggerFactory.getLogger(CapFundDivController.class);
    public static final String CAP_FUND_CHILD_DELETED = "cap:fund:child:deleted";
    public static final String CAP_FUND_CHILD_DIV = "cap:fund:child:div";
    @Autowired
    private ISysUsrService usrService;
    @Autowired
    private ICapFundDivService divService;
    @Autowired
    private ICapFundService capFundService;
    @Autowired
    private IMultiRedisService multiRedisService;
    /**
     * 资金拆分
     * 将母资金拆出一部分作为子资金
     * @param id 母资金ID
     * @param amt 拆出的数额（元）
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public synchronized DataResultDTO add(Long id,Double amt){
        String requestId = RedisLock.randomValue(16);
        boolean lock = multiRedisService.getDistributedLock(CAP_FUND_CHILD_DIV+id,requestId , 15);
        DataResultDTO res = null;
        if(lock){
            try {
                res= divService.divAndSave(id, DecimalUtils.convertYuanToFen(amt));
            }finally {
                multiRedisService.releaseDistributedLock(CAP_FUND_CHILD_DIV+id, requestId);
            }
        }
        return res;
    }

    /**
     * 初始化来款展开list
     * @param id
     * @return
     */
    @RequestMapping("initList.do")
    @ResponseBody
    public ListPageVO initList(Long id){
        ListPageVO res = null;
        CapFundQueryCondition qc = new CapFundQueryCondition();
        qc.setFundPid(id);
        List<CapFundVO> list = capFundService.searchList(qc);
        // set crtUsrNm
        if(CollectionUtils.isNotEmpty(list)){
            for (CapFundVO capFundVO : list) {
                Long crtUsrId = capFundVO.getCrtUsrId();
                SysUsrVO sysUsrVO = this.usrService.selectById(crtUsrId,"11000");
                capFundVO.setCrtUsrNm(sysUsrVO.getRefRlNm());
            }
        }
        res = new ListPageVO(list,list.size());
        return res;
    }

    /**
     * 删除拆分资金
     * @param id
     * @return
     */
    @RequestMapping("/delDiv.do")
    @ResponseBody
    public synchronized DataResultDTO delDiv(Long id){
        BaseLogUtils.info(LOGGER,"delDiv","删除子资金请求id:" +id);
        boolean exists = RedisHelper.keyExists(CAP_FUND_CHILD_DELETED + id);
        if(exists){
            BaseLogUtils.info(LOGGER,"delDiv","重复操作直接返回" +id);
            DataResultDTO res = new DataResultDTO(1,"删除成功");
            return res;
        }else{
            RedisHelper.add(CAP_FUND_CHILD_DELETED + id,"1");
            RedisHelper.expire(CAP_FUND_CHILD_DELETED + id,30);
        }
        DataResultDTO dataResultDTO = this.divService.delDiv(id);
        BaseLogUtils.info(LOGGER,"delDiv","返回删除子资金"+id+"结果" + JSON.toJSONString(dataResultDTO));
        return dataResultDTO;
    }
}
